package com.hailian.fintech.hlfinanceserver.web;


import com.github.pagehelper.PageInfo;
import com.hailian.fintech.feignserver.feign.AccountFeign;
import com.hailian.fintech.feignserver.feign.UserFeign;
import com.hailian.fintech.hlcomm_interfaceserver.entity.POJO;
import com.hailian.fintech.hlcomm_interfaceserver.result.ResultInfo;
import com.hailian.fintech.hlfinanceserver.entity.Tixian;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.hailian.fintech.hlfinanceserver.service.ITixianService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wk
 * @since 2020-05-23
 */
@RestController
@RequestMapping("/tixian")
@Slf4j
public class TixianController {

    @Autowired
    private  ITixianService iTixianService;
    @Autowired
    AccountFeign accountFeign;
    @Autowired
    private UserFeign userFeign;

    /**
     * 查询所有或通过状态查询
     * @param
     * @return
     */
    @RequestMapping("list")
    public  Object list(@RequestParam(defaultValue = "1")Integer pageNum, @RequestParam(defaultValue = "3")Integer pageSize, POJO pojo){
        PageInfo info = iTixianService.selectOwner(pageNum,pageSize,pojo);
        return new ResultInfo().success(info);
    }

    /**
     * 添加提现信息或修改
     * @param tixian
     * @return
     */
    @RequestMapping("addTixian")
    public  Object addTixian(@RequestBody Tixian tixian){
        boolean flag = iTixianService.saveOrUpdate(tixian);
        return new ResultInfo().success(flag);
    }

    /**
     * 提现审核通过
     * @param ids
     * @return
     */
    @RequestMapping("passTixian")
    public  Object passTixian(@RequestParam("ids") Integer[] ids){
        List<Tixian> tixians = iTixianService.listByIds(Arrays.asList(ids));
        log.info("================="+tixians);
        tixians.forEach(tixian -> {
            tixian.setState("审核通过");
            tixian.setTime(new Date());
            accountFeign.passAccount(tixian.getAccountId());//修改用户明细中的提现状态
            userFeign.moneyChange(tixian.getUserId(),"-",tixian.getSum());//修改用户余额
        });

        boolean flag = iTixianService.saveOrUpdateBatch(tixians);
        return new ResultInfo().success(flag);
    }

}

